import { spawnSync } from 'child_process';

let { promises } = require('fs');

async function openInChromeBrowser(host: string, port: number) {
    let chrome = [
        'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
        'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',
    ];
    for (let path of chrome) {
        try {
            await promises.stat(path);
            return spawnSync(path, [`http://${host}:${port}`]);
        } catch {
            return null;
        }
    }
}

function openInBrowser(host: string, port: number) {
    return spawnSync(`start http://${host}:${port}`);
}

export { openInBrowser };
